/**
 * 中间模块地图相关业务通用逻辑
 */
import SurroundingSearchResources from '@comp/SurroundingSearchResources'
import POILayerManagement from '@comp/POILayerManagement'

export const centerModuleWithMapMixin = {
  components: {
    SurroundingSearchResources,
    POILayerManagement
  },
  data() {
    return {
      layerAction: {}, // 图层管理点击动作对应对象参数
      mapCameraZoomConfig: {
        minZoom: 1,
        maxZoom: 100000000
      },
      mapBasemapSelectWidgetShowFlag: false,
      mapBoundsSwitchWidgetShowFlag: false,
      mapToolsBarShowFlag: false,
      mapLocationBarShowFlag: false,
      mapRestoreInitialStatusWidgetShowFlag: false,
      mapDefaultShowBoundsLevel: -1,
      frontClusterStartLevel: 17, // 混合聚合模式时前端聚合起始层级配置为17
      showPOINameOnMapLimitLevel: _const.mapConfigInfo.basicConfig.maxZoom + 10 // 配置POI名称地图展示层级为最大层级+10(即当前专题图层不展示POI名称)
    }
  },
  created() {
    this.load3DTiles() // 加载3D地图服务
    this.autonomousGetBindingLayers() // 获取专题绑定图层
  },
  mounted() {
    if (!this.clusterMode) {
      // 混合聚合模式时，进行地图相机视角移动结束事件监听
      this.$eventBus.$on('mapMoveOrZoomEnd', () => {
        this.loadPOIData(this.activeBindingLayerIds) // POI数据更新
      })
    }
    // 监听popup关闭事件，进行周边搜索组件关闭
    this.$eventBus.$on('popupCloseOnMap', () => {
      this.surroundingSearchResourcesManualClose()
    })
  },
  beforeDestroy() {
    // 事件监听卸载
    if (!this.clusterMode) this.$eventBus.$off('mapMoveOrZoomEnd') // 使用混合聚合模式时，地图相机视角移动结束事件监听卸载
    this.$eventBus.$off('popupCloseOnMap')
    // 销毁3D地图服务图层
    if (this.tilesLayer) {
      this.tilesLayer.remove(true)
      this.tilesLayer = null
    }
  },
  computed: {
    // 动态计算地图边距样式
    mapMarginStyle: function() {
      return {
        '--marginTop': _const.mapConfigInfo.effectConfig.margin.top,
        '--marginRight': _const.mapConfigInfo.effectConfig.margin.right,
        '--marginBottom': _const.mapConfigInfo.effectConfig.margin.bottom,
        '--marginLeft': _const.mapConfigInfo.effectConfig.margin.left
      }
    }
  },
  methods: {
    // 加载3D地图服务
    load3DTiles() {
      // 等待地图初始化完成后执行
      if (!this.mapComp) {
        setTimeout(() => {
          this.load3DTiles()
        }, 100)
        return
      }
      const tilesUrl = 'http://172.19.124.161:8000/mapdb/3dtiles/test/tileset.json'
      this.tilesLayer = new this.$Imap.layer.TilesetLayer({
        url: tilesUrl,
        // clampToGround: true,
        position: {
          alt: 61.6
        },
        flyTo: true
      })
      this.map.addLayer(this.tilesLayer)
    },
    // POI图层点击事件回调
    POILayerChangeHandle(data) {
      setTimeout(() => {
        this.layerAction = {
          action: data.action,
          isInit: data?.initFlag,
          layerInfo: data.layerInfo
        }
        this.activeBindingLayerIds = [...data.selectedLayerIds]
        switch (data.action) {
          case 'add':
            this.loadPOIData(this.activeBindingLayerIds)
            break
          case 'remove':
            this.reserveCurrentShowPopupPOIPropertyFlag = true
            for (const item of data.layerInfo) {
              this.mapComp.clearPOILayer(item.id)
            }
            break
        }
      })
    }
  }
}
